package auto.app.controller;


import auto.app.controller.from.ParamsAddFrom;
import auto.app.controller.from.ParamsAddFromJson;
import auto.app.controller.from.ParamsUpdateFrom;
import auto.app.controller.info.ParamsInfo;
import auto.app.controller.info.TypeListInfo;
import auto.app.enums.ParameType;
import auto.app.exetion.MainExection;
import auto.app.model.Params;
import auto.app.service.impl.ParamsServiceImpl;
import auto.app.util.CommonUtil;
import auto.app.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 请求参数信息表 前端控制器
 * </p>
 *
 * @author 杨森
 * @since 2022-07-09
 */
@RestController
@RequestMapping("/params")
@Api(value = "参数设置",tags = "参数设置")
public class ParamsController {
    @Autowired
    private ParamsServiceImpl paramsService;


    //新增参数绑定用例
    @PostMapping(value = "/add",name = "新增参数绑定用例")
    @ApiOperation(value = "新增参数绑定用例",notes = "新增参数绑定用例")
    public Result<Boolean> addParams(@Validated @RequestBody ParamsAddFrom paramsAddFrom) throws MainExection {
        System.out.println("摩羯阿婆");
        Boolean aBoolean = paramsService.addParams(paramsAddFrom);
        return Result.getInstance(Boolean.class).setData(aBoolean);
    }

    @PostMapping(value = "/addJson",name = "新增参数JSON格式")
    @ApiOperation(value = "新增参数JSON格式",notes = "新增参数JSON格式")
    public Result<Boolean> addParamsJson(@Validated @RequestBody ParamsAddFromJson paramsAddFromJson) throws MainExection {
        Boolean aBoolean = paramsService.addParamsJson(paramsAddFromJson);
        return Result.getInstance(Boolean.class).setData(aBoolean);
    }
    //对请求参数进行更改
    @ApiOperation(value = "编辑参数",notes = "编辑参数")
    @PostMapping(value = "/update",name = "编辑参数")
    public Result<Boolean> updateParams(@RequestBody ParamsUpdateFrom paramsUpdateFrom) throws MainExection {
        Boolean update = paramsService.update(paramsUpdateFrom);
        return Result.getInstance(Boolean.class).setData(update);
    }
    //查看参数详情
    @GetMapping(value = "/detail/{id}",name = "查看参数详情")
    @ApiOperation(value = "查看参数详情",notes = "查看参数详情")
    public Result<Params> detail(@PathVariable Integer id) throws MainExection {
        Params detail = paramsService.detail(id);
        return Result.getInstance(Params.class).setData(detail);
    }
    //删除参数
    @DeleteMapping(value = "/{id}",name = "删除参数")
    @ApiOperation(value = "删除参数",notes = "删除参数")
    public Result<Boolean> delete(@PathVariable("id") Integer id) throws MainExection {
        //查询id对应的参数是否存在
        Boolean delete = paramsService.delete(id);
        return Result.getInstance(Boolean.class).setData(delete);
    };
    //获取参数列表
    @GetMapping(value = "/list/{id}",name = "获取参数列表")
    @ApiOperation(value = "获取参数列表",notes = "获取参数列表")
    public Result<ParamsInfo> list(@NotNull@PathVariable Integer id) throws MainExection {
        ParamsInfo list = paramsService.list(id);
        return Result.getInstance(ParamsInfo.class).setData(list);
    }
    //获取参数类型
    @GetMapping(value = "/list/type",name = "获取类型列表")
    @ApiOperation(value = "获取类型列表",notes = "获取类型列表")
    public TypeListInfo typelist(){
        List<Map<String, String>> maps = ParameType.toList();
        TypeListInfo typeListInfo=new TypeListInfo();
        typeListInfo.setCode(200);
        typeListInfo.setMsg("请求成功");
        typeListInfo.setData(maps);
        return typeListInfo;
    }
}

